% getData.m : getData is the function called by fitting.m to get the
% required data from an excel spreadsheet, this also checks all the data
% for stuff that will throw errors up in the main program. Returns the
% dataset in the variable Data, also returns all the start values.
%
% Updated by K. Bryan to allow a high resolution flag

function [Data allData E0 DeltaGCat DeltaGInact DeltaHEq TEq err highres] = getData(filename)

% This section reads the data out of the spreadsheet, and assigns it to the
% appropriate variables.
Data = xlsread(filename,1);
err = xlsread(filename,2,'C3');
E0 = xlsread(filename,2,'C4');
DeltaGCat = xlsread(filename,2,'C6');
DeltaGInact = xlsread(filename,2,'C5');
DeltaHEq = xlsread(filename,2,'C7');
TEq = xlsread(filename,2,'C8');
highres = xlsread(filename,2,'F9');

% This checks that all the variable received a value that is true and a
% real number, otherwise it assigns them some general start values.
if isempty(err) || isnan(err)
    err = 1e-12;
end

if isempty(DeltaGCat) || isnan(DeltaGCat)
    DeltaGCat = 80000;
end

if isempty(DeltaGInact) || isnan(DeltaGInact)
    DeltaGInact = 95000;
end

if isempty(DeltaHEq) || isnan(DeltaHEq)
    DeltaHEq = 100000;
end

if isempty(TEq) || isnan(TEq)
    TEq = 320;
end

if isempty(highres) || isnan(highres)
    highres=0;
end

dataSize = size(Data);
timeSize = dataSize(1);
tempSize = dataSize(2);

Data(1,1) = 0;
i = 1;

% This section checks that every row has a time value, and if it does not
% then the row is removed.
while i<=timeSize
    if isnan(Data(i,1))
        Data(i,:) = [];
        dataSize = size(Data);
        timeSize = dataSize(1);
        tempSize = dataSize(2);
    else
        i = i + 1;
    end
end

% This checks if any of the rows have no time value, if not it removes the
% row.
i = 1;
while i <= tempSize
    if isnan(Data(1,i))
        Data(:,i) = [];
        dataSize = size(Data);
        timeSize = dataSize(1);
        tempSize = dataSize(2);
    else
        i = i + 1;
    end
end

allData = Data;
i = 1;

% This checks if any of the elements in the data are empty, if any are it
% sets them as the average of the surrounding values
DataSize = size(Data);
for i = 2:DataSize(1)
    for k = 2:DataSize(2)
        %If the cell was empty in the excel file it sets the element as a
        %NaN, so this checks for that
        if isnan(Data(i,k))
            if i == DataSize(1)
                Data(i,k) = Data(i-1,k);
            elseif i == 2
                Data(i,k) = Data(i+1,k);
            else
                Data(i,k) = (Data(i+1,k) + Data(i-1,k))/2;
            end
        end
    end
end



% This finds if there are 2 columns next to each other that have the same
% temperature, if so it averages them, this gives a better fit and makes to
% processing much faster.
while i<tempSize

    if Data(1,i) == Data(1,i+1)
        
        for j = 2:timeSize
            Data(j,i) = (Data(j,i)+Data(j,i+1))/2;
        end

        Data(:,i+1) = [];
        dataSize = size(Data);
        timeSize = dataSize(1);
        tempSize = dataSize(2);
    else
        i = i + 1;
    end


end





end
